<?php

namespace App\Admin\Controllers;

use App\Models\RechargeRecord;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class RechargeRecordController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(RechargeRecord::class, function (Grid $grid) {

            $grid->id('序号')->sortable();
            $grid->disableExport();

			$grid->filter(function($filter){
				$filter->disableIdFilter();
				$filter->like('user_id','用户的ID');
				$filter->where(function ($query) {
					$query->whereHas('user', function ($q) {
						$q->where('name', 'like', '%' . $this->input. '%');
						$q->select('users.id', 'name');
					});
				}, '用户名');
			});
			$grid->column('user_id','用户ID');
			$grid->column('sn','充值流水号');
			$grid->column('status','充值状态');
			$grid->column('type','充值方式(用户充值、系统赠送)');
			$grid->column('pay_way','支付方式');
			$grid->column('pay_amount','支付金额(单位:元)');
			$grid->column('pay_sn','支付流水号');
            $grid->created_at('充值时间');
            
            $grid->disableCreation();//禁用新建
            $grid->disableExport();//禁用导出数据按钮
            $grid->disableRowSelector();//禁用行选择checkbox
            $grid->disableActions();//禁用行操作列
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(RechargeRecord::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
